package cn.frh.order.service.impl;

import cn.frh.basic.service.impl.BaseServiceImpl;
import cn.frh.basic.util.LoginContext;
import cn.frh.basic.util.pageList;
import cn.frh.order.domain.OrderPetAcquisition;
import cn.frh.order.dto.OrderDto;
import cn.frh.order.mapper.OrderPetAcquisitionMapper;
import cn.frh.order.query.OrderPetAcquisitionQuery;
import cn.frh.order.service.IOrderPetAcquisitionService;
import cn.frh.org.domain.Employee;
import cn.frh.org.domain.Shop;
import cn.frh.org.mapper.EmployeeMapper;
import cn.frh.org.mapper.ShopMapper;
import cn.frh.pet.domain.Pet;
import cn.frh.pet.domain.SearchMasterMsg;
import cn.frh.pet.mapper.PetMapper;
import cn.frh.pet.mapper.SearchMasterMsgMapper;
import cn.frh.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public pageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.lodaByLogininfoId(logininfo.getId());
        //如果员工的店铺id不为空就是店铺管理员 否则就是平台管理员
        if (employee.getShopId()!=null){
            query.setShopId(employee.getShopId());
            Shop shop = shopMapper.loadById(employee.getId());
            //如果管理员id 不等于 员工id 说明是员工去接单
            if (shop.getAdminId()!=employee.getId()){
                query.setEmpId(employee.getId());
            }
        }
        return super.queryPage(query);
    }


    @Override
    public void cancel(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //1.将shopid置空
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());

        //2.删除宠物
        petMapper.remove(order.getPetId());
        //删除订单
        //super.remove(id);
        //3.改状态
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }

    @Override
    public void confirm(OrderDto dto) {
        //1.修改订单状态 0待处理 1已处理
        //2.修改订单的支付方式及金额
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(dto.getId());
        order.setState(1);
        order.setPaytype(dto.getPayType());
        order.setPrice(dto.getMoney());
        orderPetAcquisitionMapper.update(order);
        //3.修改寻主消息状态
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
        //4.修改宠物的成本价
        Pet pet = petMapper.loadById(order.getPetId());
        pet.setCostprice(dto.getMoney());
        petMapper.update(pet);
    }
}
